Release 10.1A: OpenEdge Getting Started:
Object-oriented Programming
Delegation
Delegation uses composition to build a class from one or more other classes without relating the classes in a hierarchy. In many cases, one class needs to invoke behavior in another class that is not part of its class hierarchy. A class does this by maintaining a reference to the other class and invoking the other class’s public methods or accessing its public data members. When a class references another class this way, it is a container class for the other class that it accesses as a delegate. Thus, delegation is a relationship between classes where one class forwards a message it cannot otherwise handle to another class (its delegate).
Because the container class delegates behavior to a separate class, the behavior is not automatically accessible from outside the container class. Unlike an inheritance relationship, delegation requires the container class to define a stub for the message in the form of a method to allow other classes to access the behavior of the delegate. Thus, the stub method in the container class is a method that is typically of the same name as the effective method in the delegate. Because a delegation relationship is established entirely by reference to the delegate, delegation offers the run-time flexibility to easily change the referenced delegate at run time without any required change to the container class, thus providing an object of the container class a form of dynamic inheritance.
Comparison with procedure-based programming
Much of this description is very similar to how procedures interact. When a procedure named
A.pruns an internal procedure in a procedure handle forB.p, it is effectively delegating the behavior of the internal procedure toB.p. In this situation,A.pis similar to the container class in the object-oriented model, andB.pto the delegate. And clearly, a third procedure namedC.pcannot invoke the delegate's internal procedure directly throughA.p, unlessA.pitself has an internal procedure or function definition that runs the effective behavior in the handle toB.p. The key distinction with classes is that the compiler verifies all of the references between classes, whether they are within the same class hierarchy or not. Thus Progress has a much more detailed and complete definition of everything that is controlled by a container class than it can have for a procedure that runs behavior defined in another procedure.
|
Copyright © 2005 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095 |